<?php

class Core_Backend_Component_Customer_Controller_Product extends Dcmp_Component_Controller {

    public function indexAction($poiid, $pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc') {
        $this->getHistory()->record();
        $list = App::getModel('customer/collection_product')
                        ->joinAccount()
                        ->filterByPoiId($poiid)
                        ->pagination($pageindex, $pagesize)
                        ->load();
        $records = $list->loadCount();
        $shop = App::getModel('foundation/poi')->load($poiid);
        return $this->viewResult(array('list' => $list, 'records' => $records, 'shop' => $shop));
    }

    public function createAction($poiid) {
        $shop = App::getModel('foundation/poi')->load($poiid);
        $categories = Kd_Data_Factory::getQueryDcl()->from('{t}foundation_catalogue_relation', 'a')
                        ->join()->inner('{t}foundation_catalogue', 'b')->on('a.catalogue_code', 'b.code')->closest()
                        ->select()->distinct()->fields('a.catalogue_code', 'a.parent_code', 'b.code', 'b.name')->closest()
                        ->fetch()
                        ->toArray();
        return $this->viewResult(array('categories' => $categories, 'shop' => $shop));
    }

    public function editAction($id) {
        $model = App::getModel('customer/product')->load($id);
        return $this->createAction($model->getPoiId())->setModel($model);
    }

    public function applyAction() {
        return $this->saveModel('draft');
    }

    public function saveAction() {
        return $this->saveModel('draft', TRUE);
    }

    public function submitAction() {
        return $this->saveModel('pending', TRUE);
    }

    public function publishPostAction($ids) {
        App::getModel('customer/product')->publish($ids);
        return $this->redirectResult($this->getHistoryUrl('customer/product/index'));
    }

    public function forbidPostAction($ids, $statusmessage) {
        App::getModel('customer/product')->forbid($ids, $statusmessage);
        return $this->redirectResult($this->getHistoryUrl('customer/product/index'));
    }

    private function saveModel($status, $return = FALSE) {
        $data = $this->getRequest()->getForm('product');
        $model = App::getModel('customer/product');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data)->setStatus($status);
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__f('share.message.saveSuccess', '{customer.label.product}', $model->getName()));
            return $return ?
                    $this->redirectResult($this->getHistoryUrl('customer/product/index')) :
                    $this->redirectResult($this->getUrl(array('id' => $model->getIdValue()), 'edit'));
        } else {
            $this->getMessager()->addError($this->__f('share.message.saveFail', '{customer.label.product}', $model->getName()))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }

}